package com.hucheng.rest.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.hucheng.common.constant.UserConstant;
import com.hucheng.common.exception.EduException;
import com.hucheng.common.response.ServiceCoreVO;
import com.hucheng.common.utils.RedisUtils;
import com.hucheng.rest.base.BaseAction;
import com.hucheng.user.query.UserQuery;
import com.hucheng.user.service.UserService;
import com.hucheng.user.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;

import java.util.UUID;


/**
 * @Author: 凉白开不加冰
 * @Version: 0.0.1V
 * @Date: 2019-06-03
 * @Description: 用户信息API
 **/
@Slf4j
@Controller("userAction")
public class UserAction extends BaseAction {

    @Reference(interfaceClass = UserService.class, check = true)
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 用户登录
     *
     * @return
     * @throws EduException
     */
    public ServiceCoreVO loginV100() throws EduException {
        String username = getRequestContentValue("username");
        String password = getRequestContentValue("password");

        UserQuery userQuery = new UserQuery();
        userQuery.setUsername(username);
        userQuery.setPassword(password);
        UserVO userVO = userService.findUserByName(userQuery);

        return response(userVO);
    }

    /**
     * 用户登出
     *
     * @return
     */
    public ServiceCoreVO logoutV100() {
        userService.logout(userId);
        return response();
    }

    /**
     * 返回默认的accessId
     *
     * @return
     */
    public ServiceCoreVO defaultAccessIdV100() {
        String defaultAccessId = RedisUtils.getString(stringRedisTemplate, UserConstant.ACCESS_KEY);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("accessId", defaultAccessId);
        return response(jsonObject);
    }

    /**
     * 刷新默认的accessId
     *
     * @return
     */
    public ServiceCoreVO refreshDefaultAccessIdV100() {
        String accessId = UUID.randomUUID().toString().replaceAll("-", "");
        RedisUtils.set(stringRedisTemplate, UserConstant.ACCESS_KEY, accessId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("accessId", accessId);
        return response(jsonObject);
    }

}
